﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
#endregion

namespace SimplePong
{
    public class LoadingScreen : Screen
    {
        //Fields
        Thread thread;
        TimeSpan time;

        Texture2D fondo;

        Texture2D ball;
        Vector2 position;
        Vector2 velocity;
        SoundEffect sound;
        SoundEffect sound2;

        Boolean play;

        #region Properties

        #endregion

        #region Initialize
        public LoadingScreen() : base()
        {
            
        }

        #endregion

        #region Public Methods
        public override void LoadContent()
        {
            fondo = GameGlobals.content.Load<Texture2D>("Textures/Loading/loading");
            ball = GameGlobals.content.Load<Texture2D>("Textures/Loading/loadingball");
            sound = GameGlobals.content.Load<SoundEffect>("Sounds/a");
            sound2 = GameGlobals.content.Load<SoundEffect>("Sounds/b");
            
        }

        public override void Initialize()
        {
            
            time = TimeSpan.FromSeconds(1);
            thread = new Thread(new ThreadStart(InitializeScreenManager));
            thread.Start();

            position = new Vector2(0,110);
            velocity = new Vector2(10, -10);

            play = true;
        }

        public override void Update()
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                GameGlobals.isExiting = true;


            


            position.X += velocity.X;
            position.Y += velocity.Y;

            if(position.X  <= 0 || position.X + ball.Width >= 800)
            {
                velocity.X *= -1;
                sound.Play();
            }
            if (position.Y <= 0 || position.Y + ball.Height >= 480)
            {
                velocity.Y *= -1;
                sound.Play();
            }



            if (position.X == 290 && position.Y == 240)
            {
                if(play)
                {
                    sound2.Play();
                    play = false;
                }
                velocity = new Vector2();
                time -= GameGlobals.gameTime.ElapsedGameTime;
            }


            if (time < TimeSpan.Zero && thread.ThreadState == ThreadState.Stopped)
                //ScreenManager.TransitionTo("MainMenu", "BlackFade");
                ScreenManager.TransitionTo("MainMenu", "BlackFade");
        }

        public override void Draw()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(fondo, Vector2.Zero, Color.White);
            spriteBatch.Draw(ball, position, Color.White);
            spriteBatch.End();
        }

        #endregion

        #region Private Methods
        private void InitializeScreenManager()
        {
            //Add all screens here
            ScreenManager.AddScreen("MainMenu", new MainMenuScreen());
            ScreenManager.AddScreen("About", new AboutScreen());
            ScreenManager.AddScreen("Help", new HelpScreen());
            ScreenManager.AddScreen("Game", new GameScreen()); 
        }

        #endregion
    }
}
